//import Vue from 'vue'
import { createApp } from "vue";

import axios from "axios";
import "font-awesome/css/font-awesome.min.css";
import qs from "qs";
import App from "./App.vue";
import {
    getCurDate,
    getLocalStorage,
    getSessionStorage,
    removeLocalStorage,
    removeSessionStorage,
    setLocalStorage,
    setSessionStorage,
} from "./common.js";
import router from "./router";
const app = createApp(App);
app.config.productionTip = false;
app.use(router).mount("#app");

//设置axios的基础url部分
axios.defaults.baseURL = "http://localhost:14000/";

// 设置响应拦截器
axios.interceptors.response.use(function(response) {
    if (response.data.code == 403) {
        location.href = '/error403';
    }
    else if (response.data.code == 401) {
        location.href = '/error401';
    }
    return response;
}, function(error) {
    console.log(error);
    return Promise.reject(error);
});

//将axios挂载到vue实例上，使用时就可以 this.$axios 这样使用了
app.config.globalProperties.$axios = axios;
app.config.globalProperties.$qs = qs;
app.config.globalProperties.$getCurDate = getCurDate;
app.config.globalProperties.$setSessionStorage = setSessionStorage;
app.config.globalProperties.$getSessionStorage = getSessionStorage;
app.config.globalProperties.$removeSessionStorage = removeSessionStorage;
app.config.globalProperties.$setLocalStorage = setLocalStorage;
app.config.globalProperties.$getLocalStorage = getLocalStorage;
app.config.globalProperties.$removeLocalStorage = removeLocalStorage;

router.beforeEach(function (to, from, next) {
    let user = sessionStorage.getItem("user");
    //除了登录、注册、首页、商家列表、商家信息之外，都需要判断是否登录
    if (
        !(
            to.path == "/" ||
            to.path == "/index" ||
            to.path == "/businessList" ||
            to.path == "/businessInfo" ||
            to.path == "/login" ||
            to.path == "/register" ||
            to.path == "/error403" ||
            to.path == "/error401"
        )
    ) {
        if (user == null) {
            router.push("/login");
            // location.reload();
        }
    }
    next();
});
app.use(router);
// app.use(table)

// app.mount("#app");
